<template>
  <div id="home" class="wrapper">
    <Navbar class="navbar"><template v-slot:center>购物街</template></Navbar>
    <tabControl
      :titles="['流行', '新款', '精选']"
      @tabclick="tabclick"
      ref="tabControl1"
      v-show="isTopFixed"
      class="fixed"
    ></tabControl>
    <scroll
      class="content"
      ref="scroll"
      :probeType="3"
      @scroll="contscroll"
      :pullUpLoad="true"
      @pullingUp="loadmore"
    >
      <swipershow
        :banners="banners"
        @swiperimageload.once="swiperimageload"
      ></swipershow>
      <recommend :recommends="recommends"></recommend>
      <feayure></feayure>
      <tabControl
        :titles="['流行', '新款', '精选']"
        @tabclick="tabclick"
        ref="tabControl2"
      ></tabControl>
      <goods :goods="goods[cunt].list"></goods>
    </scroll>
    <backtop @click.native="backclick" v-show="btnTopShow"></backtop>
  </div>
</template>

<script>
import Navbar from "components/common/navbar/navbar.vue";
import tabControl from "components/content/tabControl/tabControl";
import goods from "components/content/goods/goods";
import scroll from "components/common/scroll/scroll";

import { gethome, getHomeGoods } from "network/home.js";
import { backtopMixin } from "@/common/mixin";

import swipershow from "./child/swipershow";
import recommend from "./child/recommend";

import feayure from "views/home/child/feayure";

export default {
  name: "Home",
  mixins: [backtopMixin],
  components: {
    Navbar,
    swipershow,
    recommend,
    feayure,
    tabControl,
    goods,
    scroll,
  },
  data() {
    return {
      banners: [],
      recommends: [],
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      cunt: "pop",
      taboffsetTop: 0,
      isTopFixed: false,
      saveY: 0,
    };
  },
  created() {
    //请求轮播图数据
    this.gethome();
    //请求商品数据
    this.getHomeGoods("pop");
    this.getHomeGoods("new");
    this.getHomeGoods("sell");
  },
  activated() {
    this.$refs.scroll.scrollTo(0, this.saveY, 0.1);
    this.$refs.scroll.scroll.refresh();
  },
  deactivated() {
    this.saveY = this.$refs.scroll.scroll.y;
  },
  methods: {
    //事件监听
    tabclick(e) {
      switch (e) {
        case 0:
          this.cunt = "pop";
          break;
        case 1:
          this.cunt = "new";
          break;
        case 2:
          this.cunt = "sell";
          break;
      }
      this.$refs.tabControl1.currindex = e;
      this.$refs.tabControl2.currindex = e;
    },
    contscroll(e) {
      //决定backtop是否显示
      e.y < -1000 ? (this.btnTopShow = true) : (this.btnTopShow = false);
      //决定tabcontrol是否吸顶
      this.isTopFixed = -e.y > this.taboffsetTop;
    },
    loadmore() {
      // console.log("上拉加载");
      this.getHomeGoods(this.cunt);
    },
    swiperimageload() {
      this.taboffsetTop = this.$refs.tabControl2.$el.offsetTop;
      // console.log(this.taboffsetTop);
    },

    //网络请求
    gethome() {
      gethome()
        .then((res) => {
          // console.log(res);
          this.banners = res.data.data.banner.list;
          this.recommends = res.data.data.recommend.list;
        })
        .catch((err) => {
          this.$toast.show("网络异常", 1500);
        });
    },
    getHomeGoods(type) {
      const page = this.goods[type].page + 1;
      getHomeGoods(type, page).then((res) => {
        // console.log(res);
        this.goods[type].list.push(...res.data.data.list);
        this.goods[type].page += 1;
        this.$refs.scroll.scroll.finishPullUp();
        this.$refs.scroll.scroll.refresh();
      });
    },
  },
};
</script>
<style scoped>
#home {
  /* padding: 44px 0 39px; */
  position: relative;
  height: 100vh;
}
.navbar {
  background-color: var(--color-tint);
  color: #fff;
  letter-spacing: 2px;
}
.content {
  overflow: hidden;
  position: absolute;
  left: 0;
  right: 0;
  top: 44px;
  bottom: 49px;
  /* height: 100%; */
}
.fixed {
  position: relative;
  z-index: 9;
}
</style>
